package com.example.village.controller;

import com.example.common.utils.R;
import com.example.village.dao.MessageDao;
import com.example.village.entity.MessageEntity;
import com.example.village.entity.PayEntity;
import com.example.village.entity.UserEntity;
import com.example.village.entityVO.PayDimVO;
import com.example.village.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@Api(tags = "普通用户接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private NeighbourhoodService neighbourhoodService;

    @Autowired
    private PayService payService;

    @Autowired
    private SubscribeMsgService subscribeMsgService;

    @ApiOperation("用户信息修改")
    @RequestMapping(value = "/updateUser", method = POST)
    public R updateUser(@RequestBody UserEntity user) {
        if (userService.update(user)) {
            return R.ok("修改成功！");
        }
        return R.error("修改失败！");
    }

    @ApiOperation("查看缴费信息")
    @RequestMapping(value = "/findPayByUid", method = POST)
    public R findPayByUid(@RequestBody PayDimVO vo) {
        List<PayEntity> pays = payService.queryDimByUid(vo);
        return R.ok().put("list", pays);
    }

    @ApiOperation("支付操作")
    @RequestMapping(value = "/pay", method = POST)
    public R pay(@RequestBody PayEntity entity) {
        if (payService.update(entity)) {
            return R.ok("支付成功！");
        }
        return R.error("支付失败！");
    }

    @ApiOperation("小区消息查询")
    @RequestMapping(value = "/findMsgByNid", method = GET)
    public R findMsgByNid(Integer nId) {
        List<MessageEntity> msg = messageService.queryByNId(nId);
        return R.ok().put("list", msg);
    }

    @ApiOperation("订阅消息")
    @RequestMapping(value = "/subscribe", method = POST)
    public R subscribe(@RequestBody UserEntity user) {
        if (userService.update(user)) {
            return R.ok("订阅成功！");
        }
        return R.error("订阅失败！");
    }

}
